package cloud.maque.auth.github;

import cloud.maque.auth.annotations.ThirdLoginAnn;
import cloud.maque.auth.conf.AuthProperties;
import cloud.maque.auth.model.Oauth2AccessTokenRes;
import cloud.maque.auth.model.Oauth2GetAccesstokenReq;
import cloud.maque.auth.model.Oauth2User;
import cloud.maque.auth.service.Oauth2CallBackService;
import cloud.maque.common.core.enums.LoginType;
import cn.hutool.core.util.URLUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
@ThirdLoginAnn(value = LoginType.GITHUB)
public class Oauth2GithubCallBackServiceImpl implements Oauth2CallBackService {

    @Autowired
    AuthProperties authProperties;

    /**
     * 获取AccessToken
     *
     * @param code
     * @return
     */
    @Override
    public Oauth2AccessTokenRes getAccessToken(String code) {
        String url = "https://github.com/login/oauth/access_token";
        Oauth2GetAccesstokenReq accesstokenReq = new Oauth2GetAccesstokenReq();
        accesstokenReq.setClientId(authProperties.getGithub().getClientId());
        accesstokenReq.setClientSecret(authProperties.getGithub().getClientSecret());
        accesstokenReq.setCode(code);
        accesstokenReq.setRedirectUri(authProperties.getGithub().getRedirectUri());
        return Oauth2CallBackService.super.getAccessToken(accesstokenReq, url);
    }

    /**
     * 获取第三方的用户信息
     * <p>
     * <p>
     * {"login":"4color","id":20277116,"node_id":"MDQ6VXNlcjIwMjc3MTE2","avatar_url":"https://avatars.githubusercontent.com/u/20277116?v=4","gravatar_id":"","url":"https://api.github.com/users/4color","html_url":"https://github.com/4color","followers_url":"https://api.github.com/users/4color/followers","following_url":"https://api.github.com/users/4color/following{/other_user}","gists_url":"https://api.github.com/users/4color/gists{/gist_id}","starred_url":"https://api.github.com/users/4color/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/4color/subscriptions","organizations_url":"https://api.github.com/users/4color/orgs","repos_url":"https://api.github.com/users/4color/repos","events_url":"https://api.github.com/users/4color/events{/privacy}","received_events_url":"https://api.github.com/users/4color/received_events","type":"User","user_view_type":"public","site_admin":false,"name":"思赛宇宙","company":null,"blog":"http://www.4color.cn","location":"hangzhou","email":null,"hireable":null,"bio":null,"twitter_username":null,"notification_email":null,"public_repos":17,"public_gists":0,"followers":5,"following":4,"created_at":"2016-07-04T07:00:58Z","updated_at":"2024-12-12T09:04:56Z"}
     *
     * @param accessToken
     * @return
     */
    @Override
    public Oauth2User getUser(String accessToken) {
        String url = "https://api.github.com/user";

        HashMap<String, Object> mapHeader = new HashMap<>();
        mapHeader.put("Authorization", "Bearer " + accessToken);

        HashMap map = Oauth2CallBackService.super.getUserBase(url, mapHeader);

        Oauth2User user = new Oauth2User();
        user.setUserId(map.get("id").toString());
        user.setUserName(map.get("login").toString());
        user.setNickName(map.get("name").toString());
        return user;
    }

    /**
     * 刷新token
     *
     * @param refreshToken
     * @return
     */
    @Override
    public String refreshToken(String refreshToken) {
        return "";
    }
}
